package com.uam.utils.file.analysis.help;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.uam.common.exception.UamException;
import com.uam.common.file.CommWriterFileInfo;
import com.uam.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.*;

/**
 * @author heguanglong
 * 文件解析的
 */
@Slf4j
public class AnalysisParent {

    public void writerToFile(JSONObject jsonObject, String url) {
        String data = JSONObject.toJSONString(jsonObject);
        CommWriterFileInfo.writerStr(url, data);
    }

    public boolean writerToFile(String data, String url) {
       return CommWriterFileInfo.writerStr(url, data);
    }

    public String getFileNameGeoJson(String fileName,String dataUrl){
        String[] fName=fileName.split("\\.");
        String name=fName[0]+"-"+DateUtils.getNewDateSs();
        return dataUrl+"/"+name+".geojson";
    }


    public RetAnalysisVo toGeoJsonFile(String data, String targetUrl, String fileName) {
        try {
            List<String> typeList = new ArrayList<>(10);
            Map<String,String> dataKey=new HashMap<>(1024);
            JSONObject root = JSONObject.parseObject(data);
            JSONArray jsonArray = root.getJSONArray("features");
            if (jsonArray == null) {
                throw UamException.fileTypeError("geosjson文件格式错误，缺少features属性， name： " + fileName);
            }

            int row = jsonArray.size();
            for (int i = 0; i < row; i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                JSONObject geometry = jsonObject.getJSONObject("geometry");
                if(geometry == null){
                    continue;
                }
                JSONObject properties = jsonObject.getJSONObject("properties");
                if(properties !=null){
                    for(Map.Entry<String, Object> entry : properties.entrySet()){
                        dataKey.put(entry.getKey(),entry.getKey());
                    }
                }

                String type = geometry.getString("type");

                typeList.add(type);

                JSONArray coordinates = geometry.getJSONArray("coordinates");
                if (coordinates == null || coordinates.size() == 0) {
                    throw UamException.fileTypeError("geosjson文件格式错误，缺少coordinates属性");
                }
            }
            if (typeList.size() == 0) {
                throw UamException.fileTypeError("geosjson文件格式错误，获取点线面错误，点线面类型不能");
            }

            List<String> result = new LinkedList(new TreeSet(typeList));
            if (result.size() >3) {
                throw UamException.fileTypeError("geosjson文件格式错误，只能是点线面类型");
            }

            if(result.size() ==2){
                String type1=typeList.get(0).toUpperCase();
                String type2=typeList.get(1).toUpperCase();
                if(type1.contains(type1) || type2.contains(type1)){

                }else{
                    throw UamException.fileTypeError("数据类型错误！ geometry:" + type1+"  "+type2);
                }
            }

            int dateType = GeoTypeEnums.getCode(result.get(0));
            long fileSize=data.length();
            String dataUrl= getFileNameGeoJson(fileName,targetUrl);
            boolean flag=writerToFile(data,dataUrl);
            if(!flag){
                throw UamException.fileTypeError("写入文件错误！ " + fileName);
            }
            return new RetAnalysisVo(dateType,dataUrl,fileSize,JSONArray.toJSONString(dataKey.values()));
        } catch (Exception e) {
            log.error("文件解析错误：" + fileName, e);
            throw e;
        }
    }
}
